<?php


use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;
use App\Library\Utils\Arr;

class AnswerSeeder extends AbstractSeed
{

    public function getDependencies(): array
    {
        return [
            'QuestionSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $questions = $this->fetchAll("SELECT * FROM kg_question");
        $users = $this->fetchAll('SELECT * FROM kg_user');

        $faker = FakerFactory::create('zh_CN');

        $rows = [];
        for ($i = 0; $i < 500; $i++) {
            $rows[] = [
                'owner_id' => Arr::rand($users)['id'],
                'question_id' => Arr::rand($questions)['id'],
                'summary' => $faker->text(25),
                'content' => $faker->text(200),
                'published' => Arr::rand([1, 2]),
                'client_ip' => $faker->ipv4,
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_answer')->insert($rows)->saveData();
    }
}
